package cn.bocweb.spinning.view.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ExchargeBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.ContainsEmojiEditText;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

public class IntegralAddrActivity extends BaseActivity {

    String id;
    String userid;

    UserPresenter presenter;
    LoadingDialog dialog;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.edittext_name)
    ContainsEmojiEditText edittextName;
    @Bind(R.id.edittext_phone)
    ContainsEmojiEditText edittextPhone;
    @Bind(R.id.edittext_address)
    ContainsEmojiEditText edittextAddress;
    @Bind(R.id.textview_text)
    TextView textviewText;
    @Bind(R.id.button_submit)
    Button buttonSubmit;
    @BindColor(R.color.white)
    int white;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_addr);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        dialog = new LoadingDialog(this);

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        presenter = new UserPresenterImp();

        initToolbar();
        initIntent();

        edittextAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textviewText.setText(String.valueOf(s.toString().length()));
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    public void onEventMainThread(ExchargeBean bean) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        DeviceUtil.MyToast(this, bean.getMsg());
        Intent intent = new Intent(this, IntegralChargeActivity.class);
        startActivity(intent);
        finish();
    }

    public void onEventMainThread(String bean) {
        dialog.dismiss();
        DeviceUtil.MyToast(this, bean);

    }

    private void initIntent() {
        Intent intent = getIntent();
        id = intent.getStringExtra(Constant.TO_SHOP_ADDR_ID);
    }

    private void initToolbar() {
        toolbar.setTitle("兑换资料填写");
        toolbar.setTitleTextColor(white);
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.button_submit)
    public void onClick() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("兑换资料填写完成确认是否兑换");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String name = edittextName.getText().toString();
                String tel = edittextPhone.getText().toString();
                String address = edittextAddress.getText().toString();
                presenter.getExcharge(userid, id, name, tel, address);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }
}
